package com.example.eeuse.controller;


import com.example.eeuse.mapper.DocumentMapper;
import com.example.eeuse.model.Document;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试使用Easy-ES
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestUseEeController {
    private final DocumentMapper documentMapper;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 初始化-> 创建索引
        return documentMapper.createIndex();
    }

    @GetMapping("/insert")
    public Integer insert() {
        // 初始化-> 新增数据
        List<Document> documentList = new ArrayList<>();
        Document document = new Document();
        document.setId("1234567abcdefg");
        document.setTitle("老汉");
        document.setContent("推*技术过硬");
        documentList.add(document);

        Document document1 = new Document();
        document1.setId("1");
        document1.setTitle("java is first");
        document1.setContent("hello java");
        documentList.add(document1);

        Document document2 = new Document();
        document2.setId("2");
        document2.setTitle("C++ is first");
        document2.setContent("hello C++");
        documentList.add(document2);
        return documentMapper.insertBatch(documentList);
    }

    @PostMapping("/batchInsert")
    public Integer batchInsert(@RequestBody Document document) {
        // 初始化-> 新增数据
        documentMapper.insert(document);
        return 1;
    }

    @GetMapping("/search")
    public List<Document> search() {
        // 类似sql： title='老汉' AND (title like '%java%' OR title like '%C++%')
        LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(Document::getTitle, "老汉")
                .and(i->i.like(Document::getTitle,"java").or().like(Document::getTitle,"C++"));
//        String source = documentMapper.getSource(wrapper);
//        System.out.println(source);
        return documentMapper.selectList(wrapper);
    }

    @GetMapping("/delete")
    public int testDeleteById(String id) {
        return documentMapper.deleteById(id);
    }
}
